<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>python格式相互转换 | Sep2Sevem'blog</title><meta name="author" content="Sep2Seven"><meta name="copyright" content="Sep2Seven"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="一 字符串 1.字符串转字典（ps 字典转字符串 强转即可） ① 使用json 注意键值对要双引号 不然会报错 1234import jsona &#x3D; &amp;#x27;&amp;#123;&quot;a&quot;:&quot;1&quot;, &quot;b&quot;:&quot;1&quot;&amp;#125;&amp;#x27;c&#x3D;json.loads(a)print(c, type(c)) ② 使用eval 123">
<meta property="og:type" content="article">
<meta property="og:title" content="python格式相互转换">
<meta property="og:url" content="http://sep2seven.blog.com/2023/06/08/python%E6%A0%BC%E5%BC%8F%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2/index.html">
<meta property="og:site_name" content="Sep2Sevem&#39;blog">
<meta property="og:description" content="一 字符串 1.字符串转字典（ps 字典转字符串 强转即可） ① 使用json 注意键值对要双引号 不然会报错 1234import jsona &#x3D; &amp;#x27;&amp;#123;&quot;a&quot;:&quot;1&quot;, &quot;b&quot;:&quot;1&quot;&amp;#125;&amp;#x27;c&#x3D;json.loads(a)print(c, type(c)) ② 使用eval 123">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://sep2seven.blog.com/img/mingzuo.png">
<meta property="article:published_time" content="2023-06-08T08:50:33.000Z">
<meta property="article:modified_time" content="2023-06-08T08:34:44.117Z">
<meta property="article:author" content="Sep2Seven">
<meta property="article:tag" content="Python">
<meta property="article:tag" content="字符串与字典">
<meta property="article:tag" content="时间格式化">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://sep2seven.blog.com/img/mingzuo.png"><link rel="shortcut icon" href="/img/boniu.jpg"><link rel="canonical" href="http://sep2seven.blog.com/2023/06/08/python%E6%A0%BC%E5%BC%8F%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":true,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":2,"translateDelay":1000,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":150},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'python格式相互转换',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-06-08 16:34:44'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (false) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/boniu.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">4</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">8</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">2</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li><li><a class="site-page child" href="/picture/"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/mingzuo.png')"><nav id="nav"><span id="blog-info"><a href="/" title="Sep2Sevem'blog"><span class="site-name">Sep2Sevem'blog</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li><li><a class="site-page child" href="/picture/"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">python格式相互转换</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-06-08T08:50:33.000Z" title="发表于 2023-06-08 16:50:33">2023-06-08</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-06-08T08:34:44.117Z" title="更新于 2023-06-08 16:34:44">2023-06-08</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Python%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/">Python学习记录</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">422</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>1分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="python格式相互转换"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h4 id="一-字符串">一 字符串</h4>
<h5 id="1-字符串转字典（ps-字典转字符串-强转即可）">1.字符串转字典（ps 字典转字符串 强转即可）</h5>
<h6 id="①-使用json">① 使用json</h6>
<p>注意键值对要双引号 不然会报错</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> json</span><br><span class="line">a = <span class="string">&#x27;&#123;&quot;a&quot;:&quot;1&quot;, &quot;b&quot;:&quot;1&quot;&#125;&#x27;</span></span><br><span class="line">c=json.loads(a)</span><br><span class="line"><span class="built_in">print</span>(c, <span class="built_in">type</span>(c))</span><br></pre></td></tr></table></figure>
<h6 id="②-使用eval">② 使用eval</h6>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#容易受到恶意用户的恶意操作 比如please input:__import__(&#x27;os&#x27;).system(&#x27;dir&#x27;) 和 please input:open(&#x27;data.py&#x27;).read()</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">a = <span class="string">&#x27;&#123;&quot;a&quot;:&quot;1&quot;, &quot;b&quot;:&quot;1&quot;&#125;&#x27;</span></span><br><span class="line">b = <span class="built_in">eval</span>(a)</span><br><span class="line"><span class="built_in">print</span>(b, <span class="built_in">type</span>(b))</span><br></pre></td></tr></table></figure>
<h6 id="③-使用literal-eval">③ 使用literal_eval</h6>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> ast</span><br><span class="line">user = <span class="string">&#x27;&#123;&quot;name&quot; : &quot;john&quot;, &quot;gender&quot; : &quot;male&quot;, &quot;age&quot;: 28&#125;&#x27;</span></span><br><span class="line">user_dict = ast.literal_eval(user)</span><br><span class="line"><span class="built_in">print</span>(user_dict,<span class="built_in">type</span>(user_dict))</span><br></pre></td></tr></table></figure>
<h5 id="2-时间和字符串转化">2.时间和字符串转化</h5>
<h6 id="①-常用函数">① 常用函数</h6>
<ul>
<li>strftime 日期转字符串</li>
<li>strptime 字符串转日期</li>
<li>timedelta 时间差</li>
</ul>
<h6 id="②-相互转换">② 相互转换</h6>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">**常用参数**</span><br><span class="line"></span><br><span class="line"><span class="comment"># %y 两位数的年份表示（00-99）</span></span><br><span class="line"><span class="comment"># %Y 四位数的年份表示（000-9999）</span></span><br><span class="line"><span class="comment"># %m 月份（01-12）</span></span><br><span class="line"><span class="comment"># %d 月内中的一天（0-31）</span></span><br><span class="line"><span class="comment"># %H 24小时制小时数（0-23）</span></span><br><span class="line"><span class="comment"># %I 12小时制小时数（01-12）</span></span><br><span class="line"><span class="comment"># %M 分钟数（00=59）</span></span><br><span class="line"><span class="comment"># %S 秒（00-59）</span></span><br><span class="line"></span><br><span class="line">**获取当前时间**</span><br><span class="line"></span><br><span class="line">now=datetime.datetime.now()</span><br><span class="line"><span class="built_in">print</span>(now,<span class="built_in">type</span>(now))</span><br><span class="line"></span><br><span class="line">**时间转字符串**</span><br><span class="line"></span><br><span class="line">strtime = now.strftime(<span class="string">&#x27;%Y-%m-%d %H:%M:%S&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(strtime, <span class="built_in">type</span>(strtime))</span><br><span class="line"></span><br><span class="line">**字符串转时间**</span><br><span class="line"></span><br><span class="line">date1 = datetime.datetime.strptime(<span class="string">&#x27;2018-12-01 01:32:25&#x27;</span>, <span class="string">&#x27;%Y-%m-%d %H:%M:%S&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(date1)</span><br><span class="line"><span class="comment">#或者</span></span><br><span class="line">date2 = datetime.datetime.strptime(strtime, <span class="string">&#x27;%Y-%m-%d %H:%M:%S&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(date2)</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h6 id="③-间隔天数和秒">③ 间隔天数和秒</h6>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">**间隔天数和秒**</span><br><span class="line"></span><br><span class="line"><span class="comment">#间隔天数</span></span><br><span class="line">deltaday = (date2 - date1).days</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;间隔天数 &quot;</span>, deltaday)</span><br><span class="line"><span class="comment">#间隔秒数</span></span><br><span class="line">deltasecond = (date2 - date1).seconds</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;间隔秒数 &quot;</span>, deltasecond)</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h6 id="④-时间加减天数-使用timedelta">④ 时间加减天数 使用timedelta</h6>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#当前时间减去时间间隔</span></span><br><span class="line">repair_time = now + datetime.timedelta(days=-<span class="number">2</span>)</span><br><span class="line"><span class="comment">#格式化输出</span></span><br><span class="line"><span class="built_in">print</span>(repair_time)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://Sep2Seven.blog.com">Sep2Seven</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://sep2seven.blog.com/2023/06/08/python%E6%A0%BC%E5%BC%8F%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2/">http://sep2seven.blog.com/2023/06/08/python%E6%A0%BC%E5%BC%8F%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://Sep2Seven.blog.com" target="_blank">Sep2Sevem'blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Python/">Python</a><a class="post-meta__tags" href="/tags/%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%8E%E5%AD%97%E5%85%B8/">字符串与字典</a><a class="post-meta__tags" href="/tags/%E6%97%B6%E9%97%B4%E6%A0%BC%E5%BC%8F%E5%8C%96/">时间格式化</a></div><div class="post_share"><div class="social-share" data-image="/img/mingzuo.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/weChat.jpg" target="_blank"><img class="post-qr-code-img" src="/img/weChat.jpg" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="/img/zhifubao.jpg" target="_blank"><img class="post-qr-code-img" src="/img/zhifubao.jpg" alt="alipay"/></a><div class="post-qr-code-desc">alipay</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/06/08/python%E7%88%AC%E8%99%AB%E4%B9%8B%E5%A4%84%E7%90%86%E7%AE%80%E5%8D%95%E5%9B%BE%E7%89%87%E9%AA%8C%E8%AF%81%E7%A0%81/" title="python爬虫之百度识别图片API"><img class="cover" src="/img/mingzuo.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">python爬虫之百度识别图片API</div></div></a></div><div class="next-post pull-right"><a href="/2023/06/08/ExcelOpenpyxl/" title="Excel处理之Openpyxl库"><img class="cover" src="/img/yanhua.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Excel处理之Openpyxl库</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2023/06/08/ExcelOpenpyxl/" title="Excel处理之Openpyxl库"><img class="cover" src="/img/yanhua.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-06-08</div><div class="title">Excel处理之Openpyxl库</div></div></a></div><div><a href="/2023/06/08/python%E7%88%AC%E8%99%AB%E4%B9%8B%E5%A4%84%E7%90%86%E7%AE%80%E5%8D%95%E5%9B%BE%E7%89%87%E9%AA%8C%E8%AF%81%E7%A0%81/" title="python爬虫之百度识别图片API"><img class="cover" src="/img/mingzuo.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-06-08</div><div class="title">python爬虫之百度识别图片API</div></div></a></div></div></div><hr class="custom-hr"/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/boniu.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Sep2Seven</div><div class="author-info__description">背着昨天追赶明天，却累坏了今天</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">4</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">8</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">2</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/Sep2Seven/sep2-seven-blog"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/Sep2Seven" target="_blank" title="Gitee"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="/1074209561@qq.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">祝贺Sep2Seven个人博客开始起航</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%80-%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">1.</span> <span class="toc-text">一 字符串</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E5%AD%97%E5%85%B8%EF%BC%88ps-%E5%AD%97%E5%85%B8%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2-%E5%BC%BA%E8%BD%AC%E5%8D%B3%E5%8F%AF%EF%BC%89"><span class="toc-number">1.1.</span> <span class="toc-text">1.字符串转字典（ps 字典转字符串 强转即可）</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E2%91%A0-%E4%BD%BF%E7%94%A8json"><span class="toc-number">1.1.1.</span> <span class="toc-text">① 使用json</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E2%91%A1-%E4%BD%BF%E7%94%A8eval"><span class="toc-number">1.1.2.</span> <span class="toc-text">② 使用eval</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E2%91%A2-%E4%BD%BF%E7%94%A8literal-eval"><span class="toc-number">1.1.3.</span> <span class="toc-text">③ 使用literal_eval</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-%E6%97%B6%E9%97%B4%E5%92%8C%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E5%8C%96"><span class="toc-number">1.2.</span> <span class="toc-text">2.时间和字符串转化</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E2%91%A0-%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0"><span class="toc-number">1.2.1.</span> <span class="toc-text">① 常用函数</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E2%91%A1-%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.2.2.</span> <span class="toc-text">② 相互转换</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E2%91%A2-%E9%97%B4%E9%9A%94%E5%A4%A9%E6%95%B0%E5%92%8C%E7%A7%92"><span class="toc-number">1.2.3.</span> <span class="toc-text">③ 间隔天数和秒</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E2%91%A3-%E6%97%B6%E9%97%B4%E5%8A%A0%E5%87%8F%E5%A4%A9%E6%95%B0-%E4%BD%BF%E7%94%A8timedelta"><span class="toc-number">1.2.4.</span> <span class="toc-text">④ 时间加减天数 使用timedelta</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/06/08/ExcelOpenpyxl/" title="Excel处理之Openpyxl库"><img src="/img/yanhua.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Excel处理之Openpyxl库"/></a><div class="content"><a class="title" href="/2023/06/08/ExcelOpenpyxl/" title="Excel处理之Openpyxl库">Excel处理之Openpyxl库</a><time datetime="2023-06-08T08:50:33.000Z" title="发表于 2023-06-08 16:50:33">2023-06-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/06/08/python%E6%A0%BC%E5%BC%8F%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2/" title="python格式相互转换"><img src="/img/mingzuo.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="python格式相互转换"/></a><div class="content"><a class="title" href="/2023/06/08/python%E6%A0%BC%E5%BC%8F%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2/" title="python格式相互转换">python格式相互转换</a><time datetime="2023-06-08T08:50:33.000Z" title="发表于 2023-06-08 16:50:33">2023-06-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/06/08/python%E7%88%AC%E8%99%AB%E4%B9%8B%E5%A4%84%E7%90%86%E7%AE%80%E5%8D%95%E5%9B%BE%E7%89%87%E9%AA%8C%E8%AF%81%E7%A0%81/" title="python爬虫之百度识别图片API"><img src="/img/mingzuo.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="python爬虫之百度识别图片API"/></a><div class="content"><a class="title" href="/2023/06/08/python%E7%88%AC%E8%99%AB%E4%B9%8B%E5%A4%84%E7%90%86%E7%AE%80%E5%8D%95%E5%9B%BE%E7%89%87%E9%AA%8C%E8%AF%81%E7%A0%81/" title="python爬虫之百度识别图片API">python爬虫之百度识别图片API</a><time datetime="2023-06-08T08:50:33.000Z" title="发表于 2023-06-08 16:50:33">2023-06-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/06/08/Stream/" title="Stream流式处理"><img src="/img/yanhua.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Stream流式处理"/></a><div class="content"><a class="title" href="/2023/06/08/Stream/" title="Stream流式处理">Stream流式处理</a><time datetime="2023-06-08T07:50:33.000Z" title="发表于 2023-06-08 15:50:33">2023-06-08</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/img/mingzuo.png')"><div id="footer-wrap"><div class="copyright">&copy;2023 By Sep2Seven</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="chat-btn" type="button" title="聊天"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'EWW1EB1JHC3Trd4CP5Z6117W-gzGzoHsz',
      appKey: 'o7BwNBQKxTkjcqr8XBWekurc',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: true
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><div class="aplayer no-destroy" data-id="60198" data-server="netease" data-type="playlist" data-fixed="true" data-autoplay="true"> </div><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="188" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-nest.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-show-text.min.js" data-mobile="false" data-text="学无止境,力争上游,聚沙成塔,笨鸟先飞,勤学好问,有志竟成,绳锯木断,夕寐宵兴,搦朽磨钝" data-fontsize="15px" data-random="false" async="async"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div class="no-result" id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>